﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHTU;
using NOMFinalSettlement.DatasetReport;
using NOMFinalSettlement.CrystalReport;

namespace NOMFinalSettlement.AppForm.Program
{
    public partial class fReportNotFinishDetails : DevComponents.DotNetBar.Office2007Form
    {
        FinalSettlementModel.TableHoSo profile = null;

        public fReportNotFinishDetails()
        {
            InitializeComponent();
        }

        private void btnProcessing_Click(object sender, EventArgs e)
        {
            if (bgw.IsBusy != true)
            {
                bgw.RunWorkerAsync();
            }
        }

        private void timerLoading_Tick(object sender, EventArgs e)
        {
            circularProgress.Value += 2;
        }

        private void bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            EnableCircular(true);
            btnProcessing.Enabled = false;
            lbStatus.Text = " Đang xử lý ...";
            doSearch();
        }

        private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            EnableCircular(false);
            btnProcessing.Enabled = true;
            lbStatus.Text = "";
        }
        void EnableCircular(bool enable)
        {
            circularProgress.Visible = enable;
            circularProgress.IsRunning = enable;
            if (enable)
            {
                timerLoading.Start();
            }
            else
            {
                timerLoading.Stop();
            }
        }

        //Xử lý
        void doSearch()
        {
            DateTime _dateStart = DateTime.Now;
            dsReportNotFinishDetail reportNotFinishDetailds = new dsReportNotFinishDetail();
            var tblProfile = profile.listProfile().Where(w => w.HoanThanhQuyetToan == "Chưa hoàn thành");
            if (dateStart.Text != "")
            {
                _dateStart = dateStart.Value;
                tblProfile = tblProfile.Where(w => w.NgayNhan == _dateStart);
            }
            int stt = 1;
            foreach (var rowProfile in tblProfile)
            {
                DataRow row = reportNotFinishDetailds.dtReportNotFinishDetail.NewRow();
                row["colSTT"] = stt;
                row["colTenDuAn"] = rowProfile.TenDuAn;
                row["colChuDauTu"] = rowProfile.ChuDauTu;
                row["colDeNghiQuyetToan"] = String.Format("{0:0,0.00}", rowProfile.DeNghiGiaTriQuyetToan);
                row["colNgayNhan"] = rowProfile.NgayNhan.ToShortDateString();
                row["colCanBoThamTra"] = rowProfile.CanBo;            
                reportNotFinishDetailds.dtReportNotFinishDetail.Rows.Add(row);
                stt++;
            }
            cReportNotFinishDetails reportNotFinishDetailCr = new cReportNotFinishDetails();
            reportNotFinishDetailCr.SetDataSource(reportNotFinishDetailds);
            Invoke(new MethodInvoker(() => crystalReportViewer1.ReportSource = reportNotFinishDetailCr), null);
            reportNotFinishDetailCr.SetParameterValue("dateReport", _dateStart.ToShortDateString()); 
        }

        string IsText(Control c)
        {
            var _self = MainApp.Self.appProcess;
            return _self.crossText(c);
        }

        private void fReportNotFinishDetails_Load(object sender, EventArgs e)
        {
            profile = new FinalSettlementModel.TableHoSo();
        }
    }
}
